package com.inspector.app.ui.chat.holder;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.inspector.app.BaseApp;
import com.inspector.app.R;
import com.inspector.app.ui.chat.adapter.ChatAdapter;
import com.inspector.app.ui.chat.entity.MsgSendStatus;
import com.inspector.app.ui.chat.entity.ReceiveMessagesEntity;
import com.inspector.app.ui.widget.CircleImageView;
import com.inspector.app.ui.chat.utils.DateUtil;

import org.json.JSONObject;

public class ItemAudioSendHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    private TextView itemTime;
    private TextView name;
    private CircleImageView itemHeader;
    private RelativeLayout rlAudio;
    private TextView tvDuration;
    private ImageView ivAudio;

    private ImageView itemFail;
    private ProgressBar itemProgress;
    private String url;

    private AnimationDrawable animation;
    private ChatAdapter.OnItemListener mListener;
    private int position;
    private RelativeLayout itemLayout;
    private TextView messageRetraction;

    private ReceiveMessagesEntity mEntity;
//    private MediaPlayer mMediaPlayer;

    public ItemAudioSendHolder(@NonNull View itemView) {
        super(itemView);
//        if (mMediaPlayer == null) {
//            mMediaPlayer = new MediaPlayer();
//        }
        itemTime = itemView.findViewById(R.id.item_tv_time);
        name = itemView.findViewById(R.id.name);
        itemHeader = itemView.findViewById(R.id.chat_item_header);
        rlAudio = itemView.findViewById(R.id.rlAudio);
        tvDuration = itemView.findViewById(R.id.tvDuration);
        ivAudio = itemView.findViewById(R.id.ivAudio);

        itemFail = itemView.findViewById(R.id.chat_item_fail);
        itemProgress = itemView.findViewById(R.id.chat_item_progress);
        itemLayout = itemView.findViewById(R.id.item_layout);
        messageRetraction = itemView.findViewById(R.id.message_retraction);
    }

    public void setOnItemListener(ChatAdapter.OnItemListener listener, int position) {
        this.mListener = listener;
        this.position = position;
    }

    @SuppressLint("SetTextI18n")
    public void bind(ReceiveMessagesEntity entity) {
        if (entity != null) {
            if (entity.gettShow() == 1) {
                itemLayout.setVisibility(GONE);
                itemTime.setVisibility(GONE);
                messageRetraction.setVisibility(VISIBLE);
            } else {
                mEntity = entity;
                itemLayout.setVisibility(VISIBLE);
                itemTime.setVisibility(VISIBLE);
                messageRetraction.setVisibility(GONE);

                itemTime.setVisibility(entity.getCreateTime() == 0 ? GONE : VISIBLE);

                String standardTimes = DateUtil.getStandardTimes(entity.getCreateTime());
                itemTime.setText(standardTimes);
                rlAudio.setOnLongClickListener(this);

                name.setText(entity.getSendUserName());


                Glide.with(BaseApp.getInstance())
                        .load(entity.getSendHeadPic())
                        .apply(new RequestOptions().circleCrop())
                        .into(itemHeader);

                rlAudio.setOnClickListener(this);
                animation = (AnimationDrawable) ivAudio.getBackground();//获取动画

                MsgSendStatus sentStatus = entity.getSendStatus();
                if (sentStatus == MsgSendStatus.SENDING) {
                    itemProgress.setVisibility(VISIBLE);
                    itemFail.setVisibility(GONE);
                } else if (sentStatus == MsgSendStatus.FAILED) {
                    itemProgress.setVisibility(GONE);
                    itemFail.setVisibility(VISIBLE);
                } else if (sentStatus == MsgSendStatus.SENT) {
                    itemProgress.setVisibility(GONE);
                    itemFail.setVisibility(GONE);
                } else {
                    itemProgress.setVisibility(GONE);
                    itemFail.setVisibility(GONE);
                }
                String message = entity.getMessage();
                try {
                    JSONObject jsonObject = new JSONObject(message);
                    url = jsonObject.getString("content");
                    if (TextUtils.isEmpty(url)) return;
                    itemFail.setOnClickListener(view -> {
                        if (mListener != null) {
                            mListener.OnItemResendListener(mEntity, url, position);
                        }
                    });
//                    mMediaPlayer.setDataSource(url);
//                    mMediaPlayer.prepare();
//                    int totalDuration = mMediaPlayer.getDuration();
                    tvDuration.setText((entity.getAudioTime() / 1000) % 60 + "''");
//                    mMediaPlayer.release();
                } catch (Exception e) {

                }
            }


        }


    }

    @Override
    public void onClick(View view) {
        if (mListener != null)
            mListener.startAudioListener(animation, url, position);
    }


    @Override
    public boolean onLongClick(View view) {
        if (mListener != null) {
            mListener.OnItemLongListener(mEntity, rlAudio, position);
        }
        return true;
    }
}
